#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    test();
    btn->close();
    ini();


}

Widget::~Widget()
{
    delete ui;
}

void Widget::ini()
{
    this->setWindowTitle(tr("脚本操作器"));
    this->setFixedSize(800, 600);

}

void Widget::test()
{
    btn->setText(tr("测试"));
    connect(btn,&QPushButton::clicked,[=]{
        /*su.setMouseCoordinate(50,292);
        su.doubleCilck();//*/

        /*Route route;
        route.pushPoint(10,800,288);
        route.pushPoint(1000,100,0);
        //su.setMouseCoordinate(800,288);
        su.drag(route);//*/

        /*Sleep(1000);
        su.keyboard(10,65);//*///无论时延，只能输入单个字符
    });

}




void Widget::on_pushButton_2_clicked()
{
    int sleep = ui->csleep->text().toUInt();
    int ci = ui->cci->text().toUInt();
    Sleep(sleep);
    for(int i=0;i<ci;i++)
        su.click();
}

void Widget::on_pushButton_clicked()
{
    fileName = QFileDialog::getOpenFileName(this,
                                                        tr("文件对话框！"),
                                                        "F:",
                                                        tr("本本文件(*txt)"));
    qDebug()<<"filename : "<<fileName;
    QString currentText = ui->label->text()+":"+fileName;
    ui->label->setText(currentText);
}

void Widget::on_pushButton_3_clicked()
{
    int ci = ui->lineEdit->text().toUInt();
    if(fileName==NULL){
        QMessageBox::information(NULL, "错误", "你还没有选择文件", QMessageBox::Yes, QMessageBox::Yes);
        return;
    }
    QFile file(fileName);
    if (!file.open(QIODevice::ReadOnly)) {
        QMessageBox::information(NULL, "错误", "文件打开错误", QMessageBox::Yes, QMessageBox::Yes);
        return;
    }

    QTextStream stream(&file);
    QString data[3];
    while(1){
        stream>>data[0]>>data[1]>>data[2];
        if(data[0]==NULL)
            break;
        if(data[1]=="key"){
            int time = data[0].toInt();
            int key = data[2].toStdString()[0];
            route.pushPoint(time,key);
        }else if(data[1]=="value"){
            int time = data[0].toInt();
            int key = data[2].toInt();
            route.pushPoint(time,key);
        }else{
            int time = data[0].toInt();
            int x = data[1].toInt();
            int y = data[2].toInt();
            route.pushPoint(time, x, y);
        }
    }
    //route.show();
    for(int i=0;i<ci;i++){
        QVector<routePoint>::iterator iter;
        for(iter = route.data.begin(); iter!=route.data.end();iter++){
            Sleep(iter->time);
            if(iter->flag==true){
                POINT mouse;
                mouse.x=iter->x;
                mouse.y=iter->y;
                su.setMouseCoordinate(mouse);
                su.click();
            }else {
                su.keyboard(10,iter->key);
            }
        }
    }

    route.data.clear();
    file.close();
}
